package com.gistone.commis.api.restController;

import com.gistone.commis.database.GetBySqlMapper;
import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.Result;
import com.gistone.commis.entity.Setting;
import com.gistone.commis.mapper.SettingMapper;
import com.gistone.commis.util.DataTablePageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/8/25.
 */

@RestController("apiSettingController")
@RequestMapping("api/setting")
public class SettingController {


    private static final Logger logger = LoggerFactory.getLogger(SettingController.class);


    @Autowired
    private GetBySqlMapper getBySqlMapper;
    @Autowired
    private SettingMapper settingMapper;

    @RequestMapping(method = RequestMethod.GET, value = "/getValueByName")
    public Result getValueByName(HttpServletRequest request, HttpServletResponse response, @RequestParam String name) {
        String sql = "select setting_value from tb_setting  where   setting_name ="+ "'" + name + "'";
        List<Map> maps=this.getBySqlMapper.findRecords(sql);
        if(maps.size()>0){
            return  Result.ok(maps.get(0).get("setting_value"));
        }else{
            return  Result.build(1,"no result value for "+name);
        }
    }

   /* *//**
     * 新建权限设置信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public Object create(HttpServletRequest request, HttpServletResponse response) {


        Setting setting = new Setting();
        EditorResult<Setting> settingEditorResult = new EditorResult<>();
        setting.setSettingName(request.getParameter("data[0][settingName]"));
        setting.setSettingValue(Integer.valueOf(request.getParameter("data[0][settingValue]")));

        if (settingMapper.insertSelective(setting)> 0) {
            List<Setting> data = new ArrayList<>();
            data.add(settingMapper.selectByPrimaryKey(setting.getSettingId()))	;
            settingEditorResult.setData(data);
        } else {
            response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }


        return settingEditorResult;

    }

    /**
     * 批量修改权限设置信息， id以,号分割
     *
     * @param houseIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.PUT, value = "/edit")
    public Object edit(@RequestParam("id") String houseIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = houseIdStr.split(",");
        EditorResult<Setting> settingEditorResult = new EditorResult<>();

        List<Setting> data =new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            int settingId = Integer.valueOf(arr[i]);

            System.out.println(settingId);
            Setting setting = new Setting();

            setting.setSettingId(settingId);
            setting.setSettingName(request.getParameter("data["+settingId+"][settingName]"));
            setting.setSettingValue(Integer.valueOf(request.getParameter("data["+settingId+"][settingValue]")));


            if (settingMapper.updateByPrimaryKeySelective(setting) > 0) {
                data.add(settingMapper.selectByPrimaryKey(settingId));


            }else{
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }


        }

        settingEditorResult.setData(data);
        return settingEditorResult;


    }


    /**
     * 用户删除
     * 通过房屋ID批量删除房屋信息， id以,号分割
     *
     * @param settingIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.DELETE, value = "/remove")
    public Object remove(@RequestParam("id") String settingIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = settingIdStr.split(",");
        EditorResult<Setting> settingEditorResult = new EditorResult<>();
        for (int i = 0; i < arr.length; i++) {
            int settingId = Integer.valueOf(arr[i]);
            if (settingMapper.deleteByPrimaryKey(settingId) > 0) {
                System.out.println("删除" + settingId + "成功！");


            } else {
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }
        }
        return settingEditorResult;


    }

    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<Setting> pageDatatables(HttpServletRequest request, Setting setting) {

        System.out.println(setting.toString());

//        List<Setting> settings =settingMapper.findSettings();


        DataTablePageUtil<Setting> dataTable = new DataTablePageUtil<>(request);

        PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());
        List<Setting> list = settingMapper.selectSelective(setting);
        PageInfo<Setting> pageInfo =new PageInfo<>(list);
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面


        return dataTable;

    }


}
